/**
 * Tilera Custom JS functions
 */
// Search box value remove on focus

var swap_text_boxes = [];

function preload(images) {
    $(images).each(function(){
        $('<img/>')[0].src = this;
    });
}

function init_swap_text_boxes(){
  //Store the default value for each box
  $('#search-box input[type=text][value].swaptextbox').each(function() {
    swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
  });
  //Add focus and blur events to set or clear the value
  $('#search-box input[type=text][value].swaptextbox').bind('focus', function() {
    if($(this).val() == swap_text_boxes[$(this).attr('id')]) {
      $(this).val('');
    }
  });
  $('#search-box input[type=text][value].swaptextbox').bind('blur', function() {
    if($(this).val() == '') {
      $(this).val(swap_text_boxes[$(this).attr('id')]);
    }
  });

}


$(document).ready( function() {
	// Search Box
	$("#search .form-submit").addClass('styleme');
	
	$("li a.active","#primary").parents("li.expanded").addClass("active-trail");
	
	// Initialize search label swap
	init_swap_text_boxes();

	// Add _blank to every link that has http in it
	$('a[href^="http://"]')
		.attr({
		target: "_blank"
	});

	// hoverIntent for secondary menu
	var configHover = {
		sensitivity: 2,
		interval: 1,
		over: openNav,
		timeout: 100,
		out: closeNav
	}
	
	function openNav(){
		$(this).addClass("open");
   	}
	function closeNav() {
        $(this).removeClass("open");
    }
	
	// Open sub menu on hover	
	$("#primary ul.menu li.expanded").not("ul.menu li.expanded ul.menu li.expanded").hoverIntent(configHover);


	// Slider
	$("#slides .content ul li").fadeOut("fast");
	
	$("a","#slider-nav").hover(function(){

		$("span",this).fadeIn("slow");

		$("#slides .content ul li").fadeIn('slow').parents('#slides').stop().scrollTo( $(this).attr('rel') , 1000, { axis:'x',easing:'easeOutQuint',reset:false} );
		
		$("#slider-nav").hover(function(){},function(){			
			$("#slides .content ul li").fadeOut("fast");
		});
		
	},function(){
		$("span",this).fadeOut("fast");
	});

	
	// Initalize sliders
	$.localScroll.defaults.axis = 'x';
	$.localScroll.defaults.event = 'mouseover';
	$.localScroll.defaults.queue = false;
	$.localScroll.defaults.easing = 'easeOutQuint';
	$.localScroll.defaults.duration = 1200;
	$.localScroll.defaults.reset = false;
	
	$('#partners-overview').localScroll({axis:'y', duration: 2000, event: 'click', hash: true});
	$('#investors-overview').localScroll({axis:'y', duration: 2000, event: 'click', hash: true});
	
	
	/* Tabbed Product Briefs */
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	
	$(".processors ul.pb_tabs li:eq(0)").addClass("active").show(); //Activate first tab
	$(".platforms ul.pb_tabs li:eq(1)").addClass("active").show(); //Activate first tab
	$(".development_tools ul.pb_tabs li:eq(2)").addClass("active").show(); //Activate first tab
	$(".page-node-336 ul.pb_tabs li:eq(3)").addClass("active").show(); //Activate first tab
	$(".page-node-340 ul.pb_tabs li:eq(3)").addClass("active").show(); //Activate first tab

	$(".processors .tab_content:eq(0)").show(); //Show first tab content
	$(".platforms .tab_content:eq(1)").show(); //Show first tab content
	$(".development_tools .tab_content:eq(2)").show(); //Show first tab content
	$(".page-node-336 .tab_content:eq(3)").show(); //Show first tab content
	$(".page-node-340 .tab_content:eq(3)").show(); //Show first tab content
	 
	
	//On Click Event
	$("ul.pb_tabs li").click(function() {

		$("ul.pb_tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	if($("body").hasClass("front")){
		
		function nextTweet() { 
			var current = $('#tweets ul.tweets-pulled-listing li:visible');
			var nextIndex = $('#tweets ul.tweets-pulled-listing li').index($('#tweets ul.tweets-pulled-listing li:visible')) + 1;
			if(nextIndex >= $('#tweets ul.tweets-pulled-listing li').length){
				nextIndex = 0;
			}			
			current.hide('slow',function(){
				$('#tweets ul.tweets-pulled-listing li')
					.eq(nextIndex)
					.fadeIn();
			});
		}
	
		$('#tweets ul.tweets-pulled-listing li')
			.hide()
			.eq(0)
			.fadeIn();
		
		var tweetTimer = null;
		
		$('#tweets').hover(
			function(){
				clearInterval(tweetTimer);
				$('#tweets .tweets-pulled-listing li ')
					.css({'color':'#bbbbbb'});
			},
			function(){
				tweetTimer = setInterval(nextTweet, 6000);
				$('#tweets .tweets-pulled-listing li ')
					.css({'color':'#777777'});
			}
		);
		tweetTimer = setInterval(nextTweet, 6000);		
	}
	
	// Load Cloud Computing Video
	if($("body").hasClass("page-node-14")){
		swfobject.embedSWF("/sites/default/files/media/cc_intro_player.swf", "ccvid", "480", "382", "9.0.0", "/sites/default/files/media/expressInstall.swf");
	}
	
	var flashvars = {};
	var params = {wmode:'transparent'};
	var attributes = {};
		
});
